package cn.devzyh.service.impl;

import cn.devzyh.dao.RoleMapper;
import cn.devzyh.domain.*;
import cn.devzyh.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> findByCondition(Role role) {

        return roleMapper.findByCondition(role);
    }

    @Override
    public void saveRole(Role role) {

        Date now = new Date();
        role.setCreatedTime(now);
        role.setUpdatedTime(now);
        role.setCreatedBy("system");
        role.setUpdatedBy("system");

        roleMapper.saveRole(role);
    }

    @Override
    public void updateRole(Role role) {

        role.setUpdatedTime(new Date());
        role.setUpdatedBy("system");

        roleMapper.updateRole(role);
    }

    @Override
    public Integer[] findMenuByRoleId(Integer roleId) {

        return roleMapper.findMenuByRoleId(roleId);
    }

    @Override
    @Transactional
    public void roleContextMenu(Integer roleId, Integer[] menuIdList) {

        // 删除角色菜单关联数据
        roleMapper.deleteMenuByRoleId(roleId);

        // 添加角色菜单关联数据
        Date now = new Date();
        for (Integer menu : menuIdList) {
            RoleMenuRelation rmr = new RoleMenuRelation();
            rmr.setRoleId(roleId);
            rmr.setMenuId(menu);
            rmr.setCreatedTime(now);
            rmr.setUpdatedTime(now);
            rmr.setCreatedBy("system");
            rmr.setUpdatedBy("system");

            roleMapper.saveRoleMenu(rmr);
        }
    }

    @Override
    @Transactional
    public void deleteRole(Integer roleId) {

        // 删除角色菜单关联数据
        roleMapper.deleteMenuByRoleId(roleId);

        // 删除角色信息
        roleMapper.deleteByRoleId(roleId);
    }

    @Override
    public List<ResourceCategory> findResourceListByRoleId(Integer roleId) {

        // 查询授权的资源分类
        List<ResourceCategory> categories = roleMapper.findResourceCategoryListByRoleId(roleId);

        // 查询分类下授权的资源
        for (ResourceCategory category : categories) {
            category.setResourceList(roleMapper.findResourceListByCondition(category.getId(), roleId));
        }

        return categories;
    }

    @Override
    @Transactional
    public void roleContextResource(RoleVo roleVo) {

        // 清除历史关系数据
        roleMapper.deleteRoleResourceRelationByRoleId(roleVo.getRoleId());

        // 添加新的关系数据
        Integer[] list = roleVo.getResourceIdList();
        Date now = new Date();
        for (Integer id : list) {
            RoleResourceRelation relation = new RoleResourceRelation();
            relation.setRoleId(roleVo.getRoleId());
            relation.setResourceId(id);
            relation.setCreatedTime(now);
            relation.setUpdatedTime(now);
            relation.setCreatedBy("system");
            relation.setUpdatedBy("system");
            roleMapper.saveRoleResourceRelation(relation);
        }

    }
}
